$(function () {
    var _uploadPage = null, _thisPage = null, user = null, html = [];

    var accountPage = function () {
        this.init();
    };

    accountPage.prototype = {
        init: function () {
          try {
              this.initData();
              this.bindEvent();
          }
          catch (e) {
              $.toast(e.message);
          }
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (user && user.id > 0) {
                $.ajax({
                  url: CONFIG_MANAGER.RequstUrl + "v1/member/" + user.id,
                  type: "GET",
                  async: true,
                  headers: {"token": user.token},
                  beforeSend: function () {
                      $.showWaiting('等待中...');
                  }
                }).done(function (data) {
                  //加载数据
                  _thisPage.loadPersonalMsg(data);
                }).fail(function () {
                  $.toast("请求失败",function(){
                    redirect("login.html");
                  });
                }).always(function () {
                  $.closeWaiting();
                });
            } else {
              redirect("login.html");
              return false;
            }
        },
        loadPersonalMsg: function (data) {
            html = [];

            html.push('<div class="wlxqbg">');
            html.push('<div class="wlxqnrbg mbtopbottom">');

            html.push('<form id="form_uploader" enctype="multipart/form-data">');
            html.push('<p style="position: relative;" class="wlxqnr mbbottom">');
            html.push('<span>');
            html.push('<input id="uploadAvatar" class="a_upload" type="file" capture="camera" accept="image/png,image/gif,image/jpg,image/jpeg">');
            html.push('<b class="grzlxtx">');
            html.push('<img src="'+ getImageUrl(data.avatar,30,30) +'"/></b>');
            html.push('<i class="iconfont icon-jiantou2"></i>')
            html.push('</span>');
            html.push('头像</p>');
            html.push('</form>');

            html.push('<p id="nick" style="position:relative;" class="wlxqnr mbbottom"><span><b class="ybzt">' + data.nickname + '</b><i class="iconfont icon-jiantou2"></i></span>昵称</p>');
            html.push('<p id="phone" class="wlxqnr mbbottom"><span><b class="ybzt">' + data.phone + '</b><i class="iconfont icon-jiantou2"></i></span>手机号码</p>');
            html.push('</div>');
            html.push('<div class="wlxqnrbg mbtopbottom">');
            html.push('<p id="password" class="wlxqnr mbbottom"><span><b class="ybzt"></b><i class="iconfont icon-jiantou2"></i>\</span>密码</p>');
            html.push('<p id="address" class="wlxqnr mbbottom"><span><b class="ybzt"></b><i class="iconfont icon-jiantou2"></i></span>收货地址</p>');
            html.push('</div>');
            html.push('</div>');
            $("#main").html(html.join(''));

            //修改昵称
            $("#main").on("tap", "#nick", function () {
                redirect("editnick.html?nickname=" + data.nickname);
                return false;
            });
            //绑定或修改手机号
            $("#main").on("tap", "#phone", function () {
                if (data.phone) {
                  redirect("editphone.html?phone=" + data.phone);
                  return false;
                } else {
                  redirect("setphone.html");
                  return false;
                }
            });

            //点击跳转选择修改密码的类型，登陆密码或者支付密码
            $("#main").on("tap", "#password", function () {
      				if( data.phone ){
                      redirect("selectPasstype.html?phone=" + data.phone);
                      return false;
      				}else{
      					$.confirm("请先绑定手机号",function(e){
      						if( 0 == e ){
      							redirect("setphone.html");
      						}
      					});
      				}
            });

            $("#main").on("tap", "#address", function () {
                redirect("address.html");
                return false;
            });
        },
        bindEvent: function () {

            //点击回退
            $("#back").on("tap", function () {
                back();
            });

            //上传头像
            $("#main").on("change", "#uploadAvatar", function () {
                var that = $(this);
                console.log($(that)[0].files[0]);
                lrz($(that)[0].files[0],
                  {
                    quality: 0.7,
                    done: function (result) {
                      console.log(result);
                      $.ajax({
                          type: "POST",
                          url: CONFIG_MANAGER.RequstUrl + "v1/member/avatar/" + user.id + "/jpg",
                          async: true,
                          data: "R0lGODlhBAAEAIABANra2v///yH/C1hNUCBEYXRhWE1QPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4gPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS4zLWMwMTEgNjYuMTQ1NjYxLCAyMDEyLzAyLzA2LTE0OjU2OjI3ICAgICAgICAiPiA8cmRmOlJERiB4bWxuczpyZGY9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkvMDIvMjItcmRmLXN5bnRheC1ucyMiPiA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIiB4bW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wLy4tLCsqKSgnJiUkIyIhIB8eHRwbGhkYFxYVFBMSERAPDg0MCwoJCAcGBQQDAgEAACH5BAEAAAEALAAAAAAEAAQAAAIGRAKGmgcFADs=",
                          headers: {"token": user.token},
                      }).done(function (data) {
                        console.log(data);
                      }).fail(function (data) {
                        $.toast(data);
                      });
                    }
                  });

//				$("#form_uploader").ajaxSubmit({
//					type:"POST",
//					url: CONFIG_MANAGER.RequstUrl + "v1/member/avatar/" + user.id,
//					headers: { "token": user.token },
//					success:function(data){
//						console.log(data);
//					}
//				});

//				var uploadForm = $("#form_uploader");
//				uploadForm.action = CONFIG_MANAGER.RequstUrl + "v1/member/avatar/" + user.id;
//				uploadForm.method = "POST";
//				uploadForm.submit();


            });


        },
    };

    _thisPage = new accountPage();
});
